package com.github.postsanf.yinian.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.postsanf.yinian.BaseFragment;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.TextKitListAdapter;
import com.github.postsanf.yinian.bean.TextKit;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class MyFragmnetText extends BaseFragment {
    private String mText;
    private RecyclerView rv_textkit;
    private TextKitListAdapter adapter;
    private List<TextKit> textKits = new ArrayList<>();

    @Override
    public void onCreate(@Nullable Bundle bundle) {
        super.onCreate(bundle);
        if (getArguments() != null) {
            mText = getArguments().getString("text");
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_texteditkit_view, null);
        rv_textkit = (RecyclerView) view.findViewById(R.id.rv_textkit);
        rv_textkit.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        rv_textkit.setItemAnimator(new DefaultItemAnimator());
        adapter = new TextKitListAdapter(getActivity(), textKits);
        rv_textkit.setAdapter(adapter);
        initTextTitles();
        return view;
    }


    private void initTextTitles() {
        String urlsto = "https://api.zhuiyinanian.com/YinianProject/event/GetTextOfOneTextType";
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put("textType", mText);
        mParam.put("textID", "0");
        StringRequest req = new StringRequest(urlsto)
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            if (jsonObject.optString("msg").equals("success")) {
                                JSONArray jsonArray = jsonObject.optJSONArray("data");
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    JSONObject kit = jsonArray.optJSONObject(i);
                                    TextKit textKit = new TextKit();
                                    textKit.setTextID(kit.optInt("textID"));
                                    textKit.setTextSource(kit.optString("textSource"));
                                    textKit.setTextContent(kit.optString("textContent"));
                                    textKits.add(textKit);
                                }
                                adapter.notifyDataSetChanged();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
//                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }
}
